From 172cbefc3bcdf8b984438f07341c12f50d337ab7 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Wed, 2 Jul 2014 16:58:25 -0700 Subject: [PATCH] Added [dev-dependencies] sections to Cargo.toml If you put a dependency in [dev-dependencies], it will only be used when building (or testing) your packages, not other packages that depend on it. --- src/cargo/util/toml.rs | 109 ++++++++++++++++---------- tests/test_cargo_compile_path_deps.rs | 81 ++++++++++++++++++- 2 files changed, 146 insertions(+), 44 deletions(-) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index e220a60d5..391fd11b0 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -111,6 +111,7 @@ pub struct TomlManifest { lib: Option>, bin: Option>, dependencies: Option>, + dev_dependencies: Option> } #[deriving(Decodable,Encodable,PartialEq,Clone,Show)] @@ -128,6 +129,13 @@ impl TomlProject { } } +struct Context<'a> { + deps: &'a mut Vec, + source_id: &'a SourceId, + source_ids: &'a mut Vec, + nested_paths: &'a mut Vec +} + impl TomlManifest { pub fn to_manifest(&self, source_id: &SourceId) -> CargoResult<(Manifest, Vec)> @@ -145,47 +153,18 @@ impl TomlManifest { let mut deps = Vec::new(); - // Collect the deps - match self.dependencies { - Some(ref dependencies) => { - for (n, v) in dependencies.iter() { - let (version, source_id) = match *v { - SimpleDep(ref string) => { - (Some(string.clone()), SourceId::for_central()) - }, - DetailedDep(ref details) => { - let reference = details.branch.clone() - .or_else(|| details.tag.clone()) - .or_else(|| details.rev.clone()) - .unwrap_or_else(|| "master".to_str()); - - let new_source_id = match details.git { - Some(ref git) => { - let kind = GitKind(reference.clone()); - let loc = try!(Location::parse(git.as_slice())); - let source_id = SourceId::new(kind, loc); - // TODO: Don't do this for path - sources.push(source_id.clone()); - Some(source_id) - } - None => { - details.path.as_ref().map(|path| { - nested_paths.push(Path::new(path.as_slice())); - source_id.clone() - }) - } - }.unwrap_or(SourceId::for_central()); - - (details.version.clone(), new_source_id) - } - }; - - deps.push(try!(Dependency::parse(n.as_slice(), - version.as_ref().map(|v| v.as_slice()), - &source_id))) - } - } - None => () + { + + let mut cx = Context { + deps: &mut deps, + source_id: source_id, + source_ids: &mut sources, + nested_paths: &mut nested_paths + }; + + // Collect the deps + try!(process_dependencies(&mut cx, self.dependencies.as_ref())); + try!(process_dependencies(&mut cx, self.dev_dependencies.as_ref())); } let project = self.project.as_ref().or_else(|| self.package.as_ref()); @@ -205,6 +184,54 @@ impl TomlManifest { } } +fn process_dependencies<'a>(cx: &mut Context<'a>, + new_deps: Option<&HashMap>) + -> CargoResult<()> { + match new_deps { + Some(ref dependencies) => { + for (n, v) in dependencies.iter() { + let (version, source_id) = match *v { + SimpleDep(ref string) => { + (Some(string.clone()), SourceId::for_central()) + }, + DetailedDep(ref details) => { + let reference = details.branch.clone() + .or_else(|| details.tag.clone()) + .or_else(|| details.rev.clone()) + .unwrap_or_else(|| "master".to_str()); + + let new_source_id = match details.git { + Some(ref git) => { + let kind = GitKind(reference.clone()); + let loc = try!(Location::parse(git.as_slice())); + let source_id = SourceId::new(kind, loc); + // TODO: Don't do this for path + cx.source_ids.push(source_id.clone()); + Some(source_id) + } + None => { + details.path.as_ref().map(|path| { + cx.nested_paths.push(Path::new(path.as_slice())); + cx.source_id.clone() + }) + } + }.unwrap_or(SourceId::for_central()); + + (details.version.clone(), new_source_id) + } + }; + + cx.deps.push(try!(Dependency::parse(n.as_slice(), + version.as_ref().map(|v| v.as_slice()), + &source_id))) + } + } + None => () + } + + Ok(()) +} + #[deriving(Decodable,Encodable,PartialEq,Clone,Show)] struct TomlTarget { name: String, diff --git a/tests/test_cargo_compile_path_deps.rs b/tests/test_cargo_compile_path_deps.rs index 7b4406eb9..16cde69b3 100644 --- a/tests/test_cargo_compile_path_deps.rs +++ b/tests/test_cargo_compile_path_deps.rs @@ -70,9 +70,13 @@ test!(cargo_compile_with_nested_deps_shorthand { } "#); - p.cargo_process("cargo-build") - .exec_with_output() - .assert(); + assert_that(p.cargo_process("cargo-build"), + execs().with_stdout(format!("{} baz v0.5.0 (file:{})\n\ + {} bar v0.5.0 (file:{})\n\ + {} foo v0.5.0 (file:{})\n", + COMPILING, p.root().display(), + COMPILING, p.root().display(), + COMPILING, p.root().display()))); assert_that(&p.bin("foo"), existing_file()); @@ -81,6 +85,77 @@ test!(cargo_compile_with_nested_deps_shorthand { execs().with_stdout("test passed\n")); }) +test!(cargo_compile_with_dev_deps { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + + name = "foo" + version = "0.5.0" + authors = ["wycats@example.com"] + + [dependencies.bar] + + version = "0.5.0" + path = "bar" + + [[bin]] + + name = "foo" + "#) + .file("src/foo.rs", + main_file(r#""{}", bar::gimme()"#, ["bar"]).as_slice()) + .file("bar/Cargo.toml", r#" + [project] + + name = "bar" + version = "0.5.0" + authors = ["wycats@example.com"] + + [dev-dependencies.baz] + + version = "0.5.0" + path = "baz" + + [[lib]] + + name = "bar" + "#) + .file("bar/src/bar.rs", r#" + pub fn gimme() -> &'static str { + "zoidberg" + } + "#) + .file("bar/baz/Cargo.toml", r#" + [project] + + name = "baz" + version = "0.5.0" + authors = ["wycats@example.com"] + + [[lib]] + + name = "baz" + "#) + .file("bar/baz/src/baz.rs", r#" + pub fn gimme() -> &'static str { + "nope" + } + "#); + + assert_that(p.cargo_process("cargo-build"), + execs().with_stdout(format!("{} bar v0.5.0 (file:{})\n\ + {} foo v0.5.0 (file:{})\n", + COMPILING, p.root().display(), + COMPILING, p.root().display()))); + + assert_that(&p.bin("foo"), existing_file()); + + assert_that( + cargo::util::process(p.bin("foo")), + execs().with_stdout("zoidberg\n")); +}) + test!(no_rebuild_dependency { let mut p = project("foo"); let bar = p.root().join("bar"); -- 2.30.2